package com.aji.controller;

import com.aji.dto.Ores3AddDto;
import com.aji.dto.Ores3ChangeDto;
import com.aji.entity.*;
import com.aji.result.Result;
import com.aji.service.FileStorageService;
import com.aji.service.LocService;
import com.aji.service.Ores3MoreService;
import com.aji.vo.Ores3Vo;
import com.aji.vo.Ores3VoMore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/loc")
@Api(tags = "矿区数据管理")
@CrossOrigin
public class LocController {
    @Autowired
    LocService locService;
    @Autowired
    Ores3MoreService ores3MoreService;
    @Autowired
    FileStorageService fileStorageService;
    @GetMapping("/ores1")
    @ApiOperation("查询 一级矿区,列表")
    public Result selectOres1(){
        List<Ores1> ores1List = locService.selectOres1();
        return Result.success(ores1List);
    }

    @GetMapping("/ores2")
    @ApiOperation("查询 二级矿区,列表")
    public Result selectOres2ByOres1(Integer ores1Id){
        List<Ores2> ores2List = locService.selectOres2ByOres1(ores1Id);
        return Result.success(ores2List);
    }

    @GetMapping("/ores3")
    @ApiOperation("查询 三级矿区,列表")
    public Result selectOres3ByOres2(Integer ores1Id,Integer ores2Id){
        List<Ores3> ores3List = locService.selectOres3ByOres2(ores1Id,ores2Id);
        return Result.success(ores3List);
    }

    @Transactional
    @GetMapping("/ores3/{ores3Id}")
    @ApiOperation("查询 三级矿区，详细信息")
    public Result selectOres3ById(@PathVariable Integer ores3Id){
        Ores3Vo ores3Vo = locService.selectOres3ById(ores3Id);
        LambdaQueryWrapper<Ores3More>ores3MoreLambdaQueryWrapper = new LambdaQueryWrapper<Ores3More>().select()
                .eq(ores3Id!=null,Ores3More::getOres_3_id,ores3Id);
        Ores3More ores3More = ores3MoreService.getOne(ores3MoreLambdaQueryWrapper);
        Ores3VoMore build = Ores3VoMore.builder().ores3More(ores3More).ores3Vo(ores3Vo).build();
        return Result.success(build);
    }
    //
    @GetMapping("/all")
    @ApiOperation("查询 所有矿区列表")
    public Result selectOres(){
        List<Ores1Select> ores1Select = locService.selectAll();
        return Result.success(ores1Select);
    }
    @PutMapping("/ores1/{ores1Id}")
    @ApiOperation("修改 一级矿区")

    public Result upDateOres1ById(@PathVariable Integer ores1Id,String ores1Name){
        locService.upDateOres1ById(ores1Id,ores1Name);
        return Result.success();
    }
    @PutMapping("/ores2/{ores2Id}")
    @ApiOperation("修改 二级矿区")

    public Result upDateOres2ById(@PathVariable Integer ores2Id,String ores2Name){
        locService.upDateOres2ById(ores2Id,ores2Name);
        return Result.success();
    }
    @PutMapping("/ores3/{ores3Id}")
    @ApiOperation("修改 三级矿区")

    public Result upDateOres3ById(@PathVariable Integer ores3Id, @RequestBody Ores3ChangeDto ores3ChangeDto){
        locService.upDateOres3ById(ores3Id,ores3ChangeDto);
        return Result.success();
    }
    @PostMapping("/ores1")
    @ApiOperation("增加 一级矿区")

    public Result addOres1(String ores1Name){
        locService.addOres1(ores1Name);
        return Result.success();
    }
    @PostMapping("/ores2")
    @ApiOperation("增加 二级矿区")

    public Result addOres2(String ores2Name,Integer ores1Id){
        locService.addOres2(ores2Name,ores1Id);
        return Result.success();
    }

    @PostMapping("/ores3")
    @ApiOperation("增加 三级矿区")
    public Result addOres3(@RequestBody Ores3AddDto ores3AddDto, Integer ores2Id){
        locService.addOres3(ores3AddDto, ores2Id);
        return Result.success();
    }
//    @PostMapping("/ores3")
//    @ApiOperation("增加 三级矿区")
//    @Transactional
//    public Result addOres3(@RequestBody Ores3AddDto ores3AddDto,Integer ores2Id,List<MultipartFile> images) throws IOException {
//        if(images.size()!=3){
//            return Result.error("文件数量默认为3个");
//        }
//        locService.addOres3(ores3AddDto,ores2Id);
//        Integer id = locService.selectOres3IdByCon(ores3AddDto);
//        Ores3More ores3More = Ores3More.builder().ores_3_id(id).build();
//        for(int i=0;i<3;i++){
//            //写入临时文件用于处理
//            File filetxt = File.createTempFile("temp", null);
//            images.get(i).transferTo(filetxt);
//            //上传文件到minio
//            FileInputStream imgFileInput = new FileInputStream(filetxt);
//            String imgurl = fileStorageService.uploadFile("img/jpg", images.get(i).getOriginalFilename(), imgFileInput, images.get(i).getContentType());
//            //more库
//            if (i==0) ores3More.setUrl_1(imgurl);
//            if (i==1) ores3More.setUrl_2(imgurl);
//            if (i==2) ores3More.setUrl_3(imgurl);
//        }
//        ores3MoreService.save(ores3More);
//        return Result.success();
//    }

    @DeleteMapping("/ores1/{ores1Ids}")
    @ApiOperation("删除 一级矿区,批量,级联")
    public Result deleteByIds1(@PathVariable List<Integer> ores1Ids){
        locService.deleteByIds1(ores1Ids);
        return Result.success();
    }

    @DeleteMapping("/ores2/{ores2Ids}")
    @ApiOperation("删除 二级矿区,批量，级联")
    public Result deleteByIds2(@PathVariable List<Integer> ores2Ids){
        locService.deleteByIds2(ores2Ids);
        return Result.success();
    }

    @DeleteMapping("/ores3/{ores3Ids}")
    @Transactional
    @ApiOperation("删除 三级矿区,批量，级联")
    public Result deleteByIds3(@PathVariable List<Integer> ores3Ids){
        //删除文件
        LambdaQueryWrapper<Ores3More>ores3MoreLambdaQueryWrapper = new LambdaQueryWrapper<Ores3More>().select()
                .in(ores3Ids != null, Ores3More::getOres_3_id, ores3Ids);
        List<Ores3More> listOres3More = ores3MoreService.list(ores3MoreLambdaQueryWrapper);
        for (Ores3More o:listOres3More
             ) {
            if (o.getUrl_1()!=null) fileStorageService.delete(o.getUrl_1());
            if (o.getUrl_2()!=null) fileStorageService.delete(o.getUrl_2());
            if (o.getUrl_3()!=null) fileStorageService.delete(o.getUrl_3());
        }
        //删除more库
        ores3MoreService.remove(ores3MoreLambdaQueryWrapper);
        //删除主库
        locService.deleteByIds3(ores3Ids);

        return Result.success();
    }
}
